<?php
/**
 * 系统主框架以及公共显示区域
 * @author Bruce
 * time 2014-10-30
 */
namespace apps\common\controllers;

use libraries\DLock;
// use apis\DLockUtils;
use enums\RetCodeEnums;
use libraries\JuHeDataUtils;
use libraries\DistrictDicUtils;
use Phalcon\Mvc\Model\Resultset;
use Phalcon\Mvc\Model\Query;
use Phalcon\Db\Result\Pdo;
use Phalcon\Db;
use enums\CachePrefixEnums;
use enums\ServiceEnums;
if( ! APP_ROOT  ) return 'Direct Access Deny!';

class TestController extends \Phalcon\Mvc\Controller
{
	private $_ut = null;
	
	const DISTRICT_TABLE_NAME = 'common_district_dic';
	
	public function initialize()
	{
		$this->_ut = $this->di[ ServiceEnums::SERVICE_UNIT_TEST];
	}
	
	
	public function indexAction()
	{
		$this->dExclusiveLock();
		
		$this->districtDicLib();
		
		$this->dLockLib();
		
	}
	
	private function dExclusiveLock()
	{
		$drl = $this->di[ServiceEnums::SERVICE_DISTRIBUTION_EXCLUSIVE_LOCK];
	}
	
	
	private function districtDicLib()
	{
		$ddu = DistrictDicUtils::getInstance( $this->di );
		
		$this->_ut->run( preg_match( "/安徽省合肥市/", var_export( $ddu->getChildrenFromID( 12 ), true ) ), 
				true, 
				'getChildrenFromID(12)',
				'测试getChildrenFromID' );
		$this->_ut->run( $ddu->getChildrenFromID( 1200000 ), 
				false, 
				'getChildrenFromID(1200000)',
				'错误测试getChildrenFromID' );
		$this->_ut->run( $ddu->getChildrenFromID( -1200000 ),
				false,
				'getChildrenFromID(-1200000)',
				'错误测试getChildrenFromID' );
		
		
		$this->_ut->run( preg_match( "/安徽省合肥市/", $strRet = var_export( $ddu->getChildrenFromName( '安徽省' ), true ) ), 
				true, 
				'getChildrenFromName(\'安徽省\')',
				'测试getChildrenFromName' );
		
		$this->_ut->run( $ddu->getChildrenFromName( '安徽省asdf' ), false, 'getChildrenFromName(\'安徽省asdf\')' );
		
		
		$this->_ut->run(  ( $strRet = var_export( $ddu->getDistsWithSelsFromID( 300 ), true )) && 
				preg_match( "/北京市/", $strRet )&&
				preg_match( "/广东省韶关市/", $strRet ) &&
				preg_match( "/梅州市/", $strRet ),
				 true, 'getDistsWithSelsFromID( 300 )' );
		$this->_ut->run(  $ddu->getDistsWithSelsFromID( 30000000 ),
				false, 'getDistsWithSelsFromID( 300 )' );
		$this->_ut->run(  $ddu->getDistsWithSelsFromID( -3000000 ),
				false, 
				'getDistsWithSelsFromID( -3000000 )' );
		
		$this->_ut->run(  ( $strRet = var_export( $ddu->getDistsWithSelsFromName( '梅州市' ), true )) &&
				preg_match( "/北京市/", $strRet )&&
				preg_match( "/广东省韶关市/", $strRet )&&
				preg_match( "/梅州市/", $strRet ),
				true, 'getDistsWithSelsFromID( \'梅州市\' )' );
		
		$this->_ut->run(  ( $strRet = var_export( $ddu->getProvinces(), true )) &&
				preg_match( "/北京市/", $strRet )&&
				preg_match( "/上海市/", $strRet )&&
				preg_match( "/河南省/", $strRet ),
				true, '$getProvinces()' );
		
		$this->_ut->run(  ( $strRet = var_export( $ddu->getSiblingsFromID( 9527 ), true )) &&
				preg_match( "/内蒙古自治区巴彦淖尔市乌拉特中旗乌兰苏木/", $strRet )&&
				preg_match( "/新忽热苏木/", $strRet )&&
				preg_match( "/乌拉特中旗/", $strRet ),
				true, 'getSiblingsFromID( 9527 )' );
		$this->_ut->run( $ddu->getSiblingsFromID( 9527000 ),
				false, 'getSiblingsFromID( 9527000 )' );
		
		$this->_ut->run(  ( $strRet = var_export( $ddu->getSiblingsFromName( '石哈河镇' ), true )) &&
				preg_match( "/内蒙古自治区巴彦淖尔市乌拉特中旗乌兰苏木/", $strRet )&&
				preg_match( "/新忽热苏木/", $strRet )&&
				preg_match( "/乌拉特中旗/", $strRet ),
				true, 'getSiblingsFromName( \'石哈河镇\' )' );
		
		$this->_ut->run(  ( $strRet = var_export( $ddu->getSingleDistFromID( 97 ), true )) &&
				preg_match( "/内蒙古自治区乌海市/", $strRet )&&
				preg_match( "/97/", $strRet ),
				true, 'getSingleDistFromID( 97 )' );
		$this->_ut->run(  $ddu->getSingleDistFromID( 970000 ),
				false, 'getSingleDistFromID( 970000 )' );
		
		
		$this->_ut->run(  ( $strRet = var_export( $ddu->getSingleDistFromName( '乌海市' ), true )) &&
				preg_match( "/内蒙古自治区乌海市/", $strRet )&&
				preg_match( "/97/", $strRet ),
				true, 'getSingleDistFromName( \'乌海市\' )' );
		
		$this->_ut->run( $ddu->getWholeDistById( 1024 ),
				'天津市武清区大碱厂镇', '$ddu->getWholeDistById( 1024 )' );
		$this->_ut->run( $ddu->getWholeDistById( 1024000 ),
				false, '$ddu->getWholeDistById( 1024000 )' );
		
		echo $this->_ut->report();
		
	}
	
	/**
	 * test for dlock
	 */
	private function dLockLib()
	{
		$dLock = DLock::getInstance( $this->di );
		
		$strLockName = 'fortest_bruce' . time();
		$iResID = 1;
		$iBase = 11;
		
		$this->_ut->setTitle( 'DLock测试' );
		//test for initlock
		$this->_ut->run( ( $iRet = $dLock->initLock( $strLockName, $iResID, $iBase, DLock::INIT_FORCE_NONE ) ) >= 1, true, 
				"initLock( $strLockName, $iResID, $iBase, DLock::INIT_FORCE_NONE ) 返回值: $iRet", '测试initLock' );
		
		$this->_ut->run( ( $iRet = $dLock->initLock( $strLockName, $iResID, $iBase, DLock::INIT_FORCE_NONE ) ) >= 1, true,
				"initLock( $strLockName, $iResID, $iBase, DLock::INIT_FORCE_NONE ) 返回值: $iRet", '测试initLock' );
		
		$iBase = 22;
		$this->_ut->run( ( $iRet = $dLock->initLock( $strLockName, $iResID, $iBase, DLock::INIT_FORCE_NONE ) ) >= 1, true,
				"initLock( $strLockName, $iResID, $iBase, DLock::INIT_FORCE_NONE ) 返回值: $iRet", '测试initLock' );
		
		$this->_ut->run( ( $iRet = $dLock->initLock( $strLockName, $iResID, $iBase, DLock::INIT_FORCE ) ) >= 1, true,
				"initLock( $strLockName, $iResID, $iBase, DLock::INIT_FORCE ) 返回值: $iRet", '测试initLock' );
		
		$this->_ut->run( ( $iRet = $dLock->initLock( $strLockName, $iResID, $iBase, DLock::INIT_FORCE_NONE ) ) >= 1, true,
				"initLock( $strLockName, $iResID, $iBase, DLock::INIT_FORCE_NONE ) 返回值: $iRet", '测试initLock' );
		
		$this->_ut->run( ( $iRet = $dLock->initLock( $strLockName, $iResID, $iBase, DLock::INIT_FORCE ) ) >= 1, true,
				"initLock( $strLockName, $iResID, $iBase, DLock::INIT_FORCE_NONE ) 返回值: $iRet", '测试initLock' );
		
		//*********************************************************************************************************
		//test for getApplicants
		$this->_ut->run( ( DLock::ERR_NO_APPLICANTS == $dLock->getApplicants( $strLockName, $iResID )), true, 
				"getApplicants( $strLockName, $iResID )", '测试getApplicants' );
		
		
		//test for getNoneLockedRes
		$this->_ut->run( (($objRet = $dLock->getNoneLockedRes($strLockName, $iResID)) && is_object( $objRet ) && 
				( $objRet->locked == 0 ) && ( $objRet->left == $iBase )), 
				true, 
				"getNoneLockedRes($strLockName, $iResID)" . var_export( $objRet, true ),
				'测试getNoneLockedRes' );
		
		//test for getResInitCnt
		$this->_ut->run( ( $iRet = $dLock->getResInitCnt($strLockName, $iResID)) == $iBase, true, 
						"getResInitCnt($strLockName, $iResID) 返回值: $iRet", '测试getResInitCnt');
		
		//test for getResStatus
		$this->_ut->run( ( $objRet = $dLock->getResStatus($strLockName, $iResID)) && $objRet['name'] == $strLockName &&
				$objRet['max'] == $iBase && $objRet['left'] == $iBase, 
				true, "getResStatus($strLockName, $iResID) 返回值:" . var_export( $objRet, true ), '测试getResStatus');
		
		//************************************************************************************************************************
		
		$iApplicantID = 2;
		$iRequireNum = 3;
		//test for lockres
		$this->_ut->run( ($iRet = $dLock->lockRes($strLockName, $iResID, $iApplicantID, $iRequireNum )), 
				1, 
				"lockRes($strLockName, $iResID, $iApplicantID, $iRequireNum ) 返回值: $iRet", 
				'测试lockRes' );
		
		$this->_ut->run( ($iRet = $dLock->lockRes($strLockName, $iResID, $iApplicantID, $iRequireNum )),
				1,
				"lockRes($strLockName, $iResID, $iApplicantID, $iRequireNum ) 返回值:" . $iRet,
				'测试lockRes' );
		
		//test for consume
		$this->_ut->run( ($iRet =  $dLock->consume( $strLockName, $iResID, $iApplicantID )), $iRequireNum, 
				"consume($strLockName, $iResID, $iApplicantID ) 返回值: $iRet", 
				'测试consume' 
				);
		
		$this->_ut->run( ( $objRet = $dLock->getResStatus($strLockName, $iResID)) && $objRet['name'] == $strLockName &&
				$objRet['max'] == $iBase && $objRet['left'] == $iBase - $iRet,
				true, "getResStatus($strLockName, $iResID) 返回值:" . var_export( $objRet, true ), '测试getResStatus');
		
		$this->_ut->run( ($iRet =  $dLock->consume( $strLockName, $iResID, $iApplicantID )), DLock::ERR_CONSUME,
				"consume($strLockName, $iResID, $iApplicantID ) 返回值: $iRet",
				'测试consume'
				);
		

			
		$this->_ut->run( $iRet = $dLock->incrRes( $strLockName, $iResID, $iApplicantID, 1 ),
				1,
				"incrRes( $strLockName, $iResID, $iApplicantID, 1 ) 返回值: " . $iRet,
				'测试incrRes( $strLockName, $iResID, $iApplicantID, 1 )' );
		
		$this->_ut->run( $iRet = $dLock->incrRes( $strLockName, $iResID, $iApplicantID, 3 ),
				false,
				"incrRes( $strLockName, $iResID, $iApplicantID, 1 ) 返回值: " . $iRet,
				'测试incrRes( $strLockName, $iResID, $iApplicantID, 1 )' );
		
		$this->_ut->run( $iRet = $dLock->incrRes( $strLockName, $iResID, $iApplicantID, 1 ),
				true,
				"incrRes( $strLockName, $iResID, $iApplicantID, 1 ) 返回值: " . $iRet,
				'测试incrRes( $strLockName, $iResID, $iApplicantID, 1 )' );
		
		$this->_ut->run( ($iRet = $dLock->lockRes($strLockName, $iResID, $iApplicantID, $iRequireNum )),
				1,
				"lockRes($strLockName, $iResID, $iApplicantID, $iRequireNum ) 返回值: $iRet",
				'测试lockRes' );
		
		$this->_ut->run( ($iRet = $dLock->lockRes($strLockName, $iResID, $iApplicantID, $iRequireNum )),
				1,
				"lockRes($strLockName, $iResID, $iApplicantID, $iRequireNum ) 返回值: $iRet",
				'测试lockRes' );
		
		
		$this->_ut->run( ($objRet = $dLock->getApplicants($strLockName, $iResID)) && is_object( $objRet ), true,
				"consume($strLockName, $iResID, $iApplicantID ) 返回值: $iRet",
				'测试consume'
		);
		
		//test for getNoneLockedRes
		$this->_ut->run( (($objRet = $dLock->getNoneLockedRes($strLockName, $iResID)) && is_object( $objRet ) &&
				( $objRet->locked == $iRequireNum ) && ( $objRet->left == 21 )),
				true,
				"getNoneLockedRes($strLockName, $iResID)" . var_export( $objRet, true ),
				'测试getNoneLockedRes' );
		
		//test for getResInitCnt
		$this->_ut->run( ( $iRet = $dLock->getResInitCnt($strLockName, $iResID)) == $iBase, true,
				"getResInitCnt($strLockName, $iResID) 返回值: $iRet", '测试getResInitCnt');
		
		//test for getResStatus
		$this->_ut->run( ( $objRet = $dLock->getResStatus($strLockName, $iResID)) && $objRet['name'] == $strLockName &&
						$objRet['max'] == $iBase && $objRet['left'] == 21,
						true, "getResStatus($strLockName, $iResID) 返回值:" . var_export( $objRet, true ), '测试getResStatus');
				
				
		$this->_ut->run( ($iRet =  $dLock->consume( $strLockName, $iResID, $iApplicantID )), $iRequireNum,
				"consume($strLockName, $iResID, $iApplicantID ) 返回值: $iRet",
				'测试consume'
		);
		
		$this->_ut->run( ($iRet =  $dLock->delLock($strLockName, $iResID)), DLock::ERR_SUCCESS,
				"delLock($strLockName, $iResID) 返回值: $iRet",
				'测试delLock'
		);
		
		echo $this->_ut->report();
	}
}